Language TutorialWa game developmentOn this pageBuild your first interactive scene with WaCommunity examples and pixel art by serci_loser. Start with a square that changes color when clicked. Then add pixel art, movement, and a save file. The last example uses the same scene-building techniques to make a text input box and demonstrate file operations. Set up the project Open the Web IDE in desktop Dora SSR. Right-click Workspace, choose New, select Wa, and check create as Wa project. When you name the project, the IDE creates wa.mod, src/main.wa, and the vendor/dora bindings. In the Web IDE, enter a name and check “create as Wa project” (shown in Chinese). Each lesson uses a different example with its own init entry point. The snippets show the relevant parts of each file. To try a lesson, copy its full source into src/main.wa, save, and select Run. Use a keyboard and an input method for the last lesson. You can also run a Wa project from the terminal. 1. Draw a square and respond to a click 1.1 Add a square to the UI from the entry point import "dora" brings in the engine bindings. At startup, Dora runs init, which calls Step2 to build the scene. Start by declaring the square's color state and its label at the top of the file. main1.waimport "dora"// 全局变量:点击回调里要改它,放全局最简单。global squareClicked = false // 方块是不是已经被点过(决定颜色)global countLabel: *dora.Label // 方块上的文字,点击后要改它// 入口:想运行哪一步,就在 init 里调用哪一步。func init { Step2()} Inside Step2, get the UI root with Director.GetUI(). Give the square a 180 × 180 Node to receive clicks, then add a DrawNode child to draw its rectangle. main1.waui := dora.Director.GetUI()square := dora.NewNode()square.SetSize(dora.NewSize(180, 180))square.SetAnchor(dora.NewVec2(0.5, 0.5))square.SetPosition(dora.Vec2Zero)square.SetTouchEnabled(true)draw := dora.NewDrawNode()rect := dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(180, 180))draw.DrawRect(rect, dora.NewColor(255, 255, 255, 255), 0, dora.ColorTransparent)square.AddChild(draw.Node)square.SetColor(dora.NewColor(80, 180, 255, 255)) // 先染成蓝色ui.AddChild(square) The anchor (0.5, 0.5) aligns the square's center with the position passed to SetPosition. In this UI, dora.Vec2Zero is the center. The rectangle uses the square's local coordinates, so drawing from the bottom-left corner (0, 0) fills the hit area. The rectangle starts white, and SetColor tints it blue. 1.2 Show a label and toggle the color Still in Step2, create the label and add it to ui. The file already declares countLabel, so this line assigns it with =. Use := when declaring a local variable such as draw in the previous step. main1.wacountLabel = dora.NewLabel("sarasa-mono-sc-regular", 40, false)if countLabel == nil { panic("创建 Label 失败,可能是字体名不对")}countLabel.SetText("点我")countLabel.SetPosition(dora.Vec2Zero) // 也放屏幕中心,盖在方块上面ui.AddChild(countLabel.Node) Register a callback with OnTapped. It runs after a full press and release, flips squareClicked, and updates the square and label. The callback does not need the touch position, so _ = touch explicitly discards the parameter. main1.wasquare.OnTapped(func(touch: dora.Touch) { _ = touch // 这一步用不到触点位置,先标记一下(凹语言要求变量被使用) squareClicked = !squareClicked // 翻转状态 if squareClicked { square.SetColor(dora.NewColor(255, 150, 70, 255)) // 橙色 countLabel.SetText("点到了!") } else { square.SetColor(dora.NewColor(80, 180, 255, 255)) // 蓝色 countLabel.SetText("点我") }}) Try it. Run main1.wa. A blue square should appear in the center. Click it once to turn it orange and change the label to 点到了!. Click it again to restore the blue square. Before clicking on the left, after clicking on the right. 2. Load pixel art and save the color 2.1 Add the grid and zombie to the scene Create an Image folder at the project root, next to wa.mod. Save the grid background and the zombie sprite in that folder using the filenames below. backgroundpng.png zombie.png In Step3 of main2.wa, use NewSpriteWithFile to create each sprite from its PNG. Size the grid to the current viewport. Leave the zombie at its original size and position it to the right and below center. If a sprite fails to load, the error identifies the path to check. main2.wafunc Step3() { ui := dora.Director.GetUI() // dora.App.GetVisualSize():返回当前屏幕的宽高(Size)。 size := dora.App.GetVisualSize() // dora.NewSpriteWithFile(图片路径):用图片文件创建一个精灵(能显示图片)。 // 文件不存在时返回 nil,所以一定要判断。 // 注意:文件名是 backgroundpng.png(如果改成了 background.png,这里也要改)。 bg := dora.NewSpriteWithFile("Image/backgroundpng.png") if bg == nil { panic("找不到背景图 Image/backgroundpng.png") } bg.SetSize(size) // 把背景图拉伸成和屏幕一样大 bg.SetAnchor(dora.NewVec2(0.5, 0.5)) bg.SetPosition(dora.Vec2Zero) ui.AddChild(bg.Node) zombie := dora.NewSpriteWithFile("Image/zombie.png") if zombie == nil { panic("找不到 Image/zombie.png") } zombie.SetAnchor(dora.NewVec2(0.5, 0.5)) zombie.SetPosition(dora.NewVec2(240, -60)) // 放在场景右边 ui.AddChild(zombie.Node)} init calls Step3 before Step2, so the square and button appear above the background. This version attaches the label to the square, so it follows when the move button changes the square's X coordinate. Color changes tint the draw node alone. 2.2 Write the color state to a file The click handler now calls saveState() after updating the UI. Save "1" for orange or "0" for blue. The example's writablePath helper joins the name with Dora's writable directory so both operations address the same file. main2.wafunc saveState() { // dora.Content.Save(文件名, 文字内容):把文字写进文件,返回是否成功。 if squareClicked { dora.Content.Save(saveFile, "1") } else { dora.Content.Save(saveFile, "0") }}// 读档:如果文件存在就恢复状态。func loadState() { // dora.Content.Exist(文件名):判断文件/目录是否存在。 // 先判断一下,避免 Load 一个不存在的文件(会报错)。 if dora.Content.Exist(saveFile) { // dora.Content.Load(文件名):读文件里的文字,文件不存在返回 nil。 loaded := dora.Content.Load(saveFile) if loaded != nil && *loaded == "1" { squareClicked = true } else { squareClicked = false } } else { squareClicked = false } applySquareState()} At startup, check whether the save file exists, read it, and call applySquareState to update the square and label. Content.Load returns a pointer to a string, so check it for nil before reading *loaded. Try it. Run main2.wa. Click Move right to move the square and label 40 units to the right. Click the square to change its color, then stop and run the example again. It restores the last saved color. This lesson saves only the color. After changing the color and pressing the move button twice. The 16 × 16 zombie is farther to the right. 3. Add movement and save the position 3.1 Handle held inputs every frame Keep the two PNGs from the previous lesson. In Step4 of main3.wa, one button moves the square right by changing its X coordinate on each click. The other sets a flag when pressed and clears it on release. main3.waleftBtn := makeButton("按住←左移", -270, -250)ui.AddChild(leftBtn)// OnTapBegan:手指按下瞬间触发;OnTapEnded:手指松开瞬间触发。// 用它们记录"是否正被按住",真正移动放到下面的主循环里。leftBtn.OnTapBegan(func(touch: dora.Touch) { _ = touch longPressLeft = true})leftBtn.OnTapEnded(func(touch: dora.Touch) { _ = touch longPressLeft = false}) Use Schedule to update continuous movement. Multiply speed in units per second by the elapsed seconds dt to get this frame's distance. Holding W increases Y, and holding the left button decreases X. Returning false keeps the callback scheduled for the next frame. main3.wasquare.Schedule(func(dt: f64) => bool { speed := f32(320) * f32(dt) // 每秒 320 个坐标单位 p := square.GetPosition() // Keyboard.IsKeyDown(键名):按住时每帧为 true。 // KeyW 就是键盘上的 W 键。 if dora.Keyboard.IsKeyDown(dora.KeyW) { p.Y = p.Y + speed // 向上 } if longPressLeft { // 长按按钮被按住 p.X = p.X - speed // 向左 } square.SetPosition(p) return false}) 3.2 Save the color and position together When the player selects Save, write the color and integer X and Y coordinates to one line, such as 1,40,0. Load splits the line on commas and checks all three fields before changing the square. Startup calls the same load function. main3.wafunc saveState() => bool { color := "0" if squareClicked { color = "1" } p := square.GetPosition() x := strconv.FormatInt(int64(i32(p.X)), 10) y := strconv.FormatInt(int64(i32(p.Y)), 10) return dora.Content.Save(saveFile, color+","+x+","+y)}// 只有三个字段都有效时才恢复方块状态。func loadState() => bool { if !dora.Content.Exist(saveFile) { return false } loaded := dora.Content.Load(saveFile) if loaded == nil { return false } fields := strings.Split(*loaded, ",") if len(fields) != 3 || (fields[0] != "0" && fields[0] != "1") { return false } x, xErr := strconv.ParseInt(fields[1], 10, 32) y, yErr := strconv.ParseInt(fields[2], 10, 32) if xErr != nil || yErr != nil { return false } squareClicked = fields[0] == "1" square.SetPosition(dora.NewVec2(f32(x), f32(y))) applySquareState() return true} Try it. Run main3.wa and try Move right one step (向右一步), Hold to move left (按住←左移), and W. Move the square and change its color, then select Save (存档). Change both again, select Load (读档), and check that both values are restored. The “Loaded” status appears after restoring the saved position and color. 4. Read and write files, then accept text input The entry point in main4.wa calls Step5 and Step6 to show the file operation results and the input box in one scene. 4.1 Create, read, and remove the demo file In Step5, put data.txt in a dedicated wa-tutorial-file-demo folder inside Dora's writable directory. If the file already exists, skip the demonstration. After writing successfully, read the text, show it on the screen, and call Remove to delete the file this run created. main4.wadir := writablePath("wa-tutorial-file-demo")file := writablePath("wa-tutorial-file-demo/data.txt")if !dora.Content.Isdir(dir) && !dora.Content.Mkdir(dir) { addLabel(ui, "创建目录失败", 24, 0, 220) return}if dora.Content.Exist(file) { addLabel(ui, "文件已存在,跳过演示", 24, 0, 220) return}if !dora.Content.Save(file, "你好,凹语言!") { addLabel(ui, "写入失败", 24, 0, 220) return}loaded := dora.Content.Load(file)if loaded != nil { addLabel(ui, "读到了: "+*loaded, 24, 0, 220)}if dora.Content.Remove(file) && !dora.Content.Exist(file) { addLabel(ui, "已删除演示文件", 24, 0, 185)} else { addLabel(ui, "删除演示文件失败", 24, 0, 185)} 4.2 Update the label with input method events Step6 builds the input box from a node, a rectangle, and a label. On a click, AttachIme lets the node receive text input. OnTextInput delivers committed text, which may contain several characters at once. Append it to inputText, then call updateInputLabel to display the result. main4.wabox.OnTapped(func(touch: dora.Touch) { _ = touch box.AttachIme() // AttachIme():让这个节点能收到 TextInput 事件})// OnTextInput(func(text)):每敲出一个字,就把这个字(字符串)送进来,我们追加到末尾。box.OnTextInput(func(text: string) { inputText = inputText + text updateInputLabel()}) 4.3 Handle Backspace and Enter Use IsKeyPressed to handle a key only on the frame it is pressed. Backspace removes the final Unicode code point. Enter clears the input. main4.wabox.Schedule(func(dt: f64) => bool { // IsKeyPressed:按下瞬间只触发一次(适合"按一下删一个字")。 if dora.Keyboard.IsKeyPressed(dora.KeyBackSpace) { inputText = removeLastChar(inputText) // 删除最后一个字 updateInputLabel() } if dora.Keyboard.IsKeyPressed(dora.KeyReturn) { inputText = "" // 回车 = 清空 updateInputLabel() } return false}) Wa strings store UTF-8 bytes. To remove a multibyte character such as 好, walk backward over its continuation bytes until you reach the first byte of the code point, then truncate the string at that position. main4.wafunc removeLastChar(s: string) => string { n := len(s) if n == 0 { return s } i := n - 1 // UTF-8 里,连续字节是 10xxxxxx(0x80~0xBF)。往前跳过连续字节,找到最后一个字符的开头。 for i > 0 && (s[i]&0xC0) == 0x80 { i-- } return s[:i]} Try it. Run main4.wa and check the text read from the file and the deletion status. Click the box, enter Wa你好, and press Backspace once. The label should show Wa你. Press Enter to clear it. The file operation results appear above the input box, which shows committed text. Full source Expand an example to copy it with all its comments. Each example can be used on its own as src/main.wa. Full source main1.wa// 社区示例与美术素材 / Community examples and pixel art: serci_loser// ===================================================================// 第 2 步:方块、文字与点击// 学什么:画一个方块、在它上面放文字、点击它换颜色// ===================================================================//// 和 lua(LÖVE) 的对照:// love.graphics.rectangle("fill", ...) → dora.NewDrawNode() + DrawRect()// love.graphics.print(...) → dora.NewLabel() + SetText()// love.mousepressed(...) → Node.OnTapped()(按下并松开算一次点击)//// 运行后:屏幕中间有一个蓝色方块,上面写着"点我"。// 点一下方块:它变橙色,文字变成"点到了!";再点一下变回来。import "dora"// 全局变量:点击回调里要改它,放全局最简单。global squareClicked = false // 方块是不是已经被点过(决定颜色)global countLabel: *dora.Label // 方块上的文字,点击后要改它// 入口:想运行哪一步,就在 init 里调用哪一步。func init { Step2()}// 第 2 步:方块、文字、点击。func Step2() { // ---- 1. 拿 2D 界面根节点 -------------------------------------- // 所有要显示的东西都要挂到它(或它的子节点)下面。 ui := dora.Director.GetUI() // ---- 2. 画一个方块 -------------------------------------------- // 空节点本身看不见,它靠"子节点"来显示内容,也负责接收点击。 square := dora.NewNode() // 给节点一个 180×180 的"命中区域"(同时也是它子节点的坐标系范围)。 square.SetSize(dora.NewSize(180, 180)) // 锚点决定节点"自己矩形"的哪个点落在 SetPosition 给的坐标上。 // (0.5,0.5) = 中心,这样 (0,0) 就是方块中心,好算。 square.SetAnchor(dora.NewVec2(0.5, 0.5)) // DoraSSR 原点在【屏幕中心】,所以 (0,0) 就是正中间。 square.SetPosition(dora.Vec2Zero) // 打开触摸,这个方块才能收到点击事件。 square.SetTouchEnabled(true) // 方块的外观:画一个白色矩形,颜色稍后用 SetColor 染。 draw := dora.NewDrawNode() // 有尺寸的节点,子节点坐标原点是它的"左下角",不是中心! // 所以从 (0,0) 画 180×180,正好铺满整个方块。 rect := dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(180, 180)) // dora.DrawNode.DrawRect(rect, fill, borderWidth, borderColor) 位于 draw_node.wa:147 // 这里画白色,是因为下一步 SetColor 会把白色"染成"想要的颜色。 draw.DrawRect(rect, dora.NewColor(255, 255, 255, 255), 0, dora.ColorTransparent) square.AddChild(draw.Node) // 给节点染色,子节点(白色方块)会跟着变成这个颜色。 square.SetColor(dora.NewColor(80, 180, 255, 255)) // 先染成蓝色 // 把方块挂到界面上。 ui.AddChild(square) // ---- 3. 放一行文字(盖在方块上) -------------------------------- countLabel = dora.NewLabel("sarasa-mono-sc-regular", 40, false) if countLabel == nil { panic("创建 Label 失败,可能是字体名不对") } countLabel.SetText("点我") countLabel.SetPosition(dora.Vec2Zero) // 也放屏幕中心,盖在方块上面 ui.AddChild(countLabel.Node) // 下面再来一行提示。 hint := dora.NewLabel("sarasa-mono-sc-regular", 28, false) if hint != nil { hint.SetText("点击方块") hint.SetPosition(dora.NewVec2(0, -140)) ui.AddChild(hint.Node) } // ---- 4. 点击 ------------------------------------------------ // 参数是一个回调函数:完整点击(按下并松开)后触发。 square.OnTapped(func(touch: dora.Touch) { _ = touch // 这一步用不到触点位置,先标记一下(凹语言要求变量被使用) squareClicked = !squareClicked // 翻转状态 if squareClicked { square.SetColor(dora.NewColor(255, 150, 70, 255)) // 橙色 countLabel.SetText("点到了!") } else { square.SetColor(dora.NewColor(80, 180, 255, 255)) // 蓝色 countLabel.SetText("点我") } }) // ---- 小结 --------------------------------------------------- // 显示东西的三步:1 new 出来 → 2 设置 → 3 AddChild 挂到 ui。 // 点击:给节点 SetSize + SetTouchEnabled,再 OnTapped 挂回调。} Full source main2.wa// 社区示例与美术素材 / Community examples and pixel art: serci_loser// ===================================================================// 第 3~4 步:绘制图片 + 存档保存// 学什么:// 第 3 步 用图片(背景、僵尸)// 第 4 步 存档(把状态写进文件,重启后读回来)// ===================================================================//// 和 lua(LÖVE) 的对照:// love.graphics.newImage + draw → dora.NewSpriteWithFile + AddChild// love.filesystem.write / read → dora.Content.Save / Load//// 运行后:背景 + 僵尸 + 蓝色方块。// 点方块:换颜色并保存;点"向右移动"按钮:方块往右走一段。import "dora"global squareFill: dora.DrawNode // 方块底色global square: dora.Node // 方块节点(第2步创建)global squareClicked = false // 方块状态:false=没点过,true=点过global countLabel: *dora.Label // 方块上的文字global saveFile = "wa-tutorial-2-color.txt" // 存档文件名// 入口。调用顺序 = 绘制顺序:先挂的在下层,后挂的在上层。func init { saveFile = writablePath(saveFile) Step3() // 先画背景、僵尸 Step2() // 再画方块、按钮 Step4() // 最后读存档}// 第 2 步:方块、文字、点击、按钮。func Step2() { ui := dora.Director.GetUI() // 拿到 2D 界面根节点,之后所有东西都挂它下面 // ---- 方块 ----------------------------------------------------- // dora.NewNode():创建一个空节点。它本身看不见,用来当容器(挂子节点)或点击区域。 square = dora.NewNode() // square.SetSize(宽, 高):设置节点矩形的大小。这里 180×180,同时也当作点击区域。 square.SetSize(dora.NewSize(180, 180)) // dora.NewSize(宽,高):定义一个尺寸 // dora.NewVec2(x, y):创建一个二维坐标(点)。 // square.SetAnchor(x, y):设置锚点,即节点矩形上哪个点会落在 SetPosition 的坐标上; // (0.5, 0.5) 表示"中心点"。 square.SetAnchor(dora.NewVec2(0.5, 0.5)) // square.SetPosition(x, y):把节点放到指定坐标。 // DoraSSR 原点在【屏幕中心】,X 向右为正,Y 向上为正,所以 (0,0) 就是正中间。 // dora.Vec2Zero 是现成的 (0,0) 点。 square.SetPosition(dora.Vec2Zero) // square.SetTouchEnabled(true):打开触摸,这个节点才能收到点击事件。 square.SetTouchEnabled(true) // ui.AddChild(子节点):把子节点挂到父节点下面,这样才会被显示/更新。 ui.AddChild(square) // dora.NewDrawNode():创建一个画板节点,可以往上面画图形。 squareFill = dora.NewDrawNode() // squareFill.DrawRect(矩形, 填充色, 描边宽, 描边色):画一个矩形。 // 描边宽填 0 表示不要描边。 // dora.NewRect(左下角坐标, 尺寸):定义一个矩形区域。 // dora.NewColor(红, 绿, 蓝, 不透明度):各是 0~255 的整数。 // dora.ColorTransparent 是"完全透明"的现成颜色。 // 注意:有尺寸的节点,子节点的坐标原点是它的【左下角】,不是中心! // 所以从 (0,0) 画 180×180,正好铺满整个方块。 squareFill.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(180, 180)), dora.NewColor(255, 255, 255, 255), 0, dora.ColorTransparent, ) square.AddChild(squareFill.Node) // squareFill.SetColor(颜色):只给方块底色的绘制节点染色。 // 所以刚才把方块画成白色,这里一染就成蓝色了。 squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // dora.NewLabel(字体名, 字号, 是否SDF):创建一行文字;找不到字体会返回 nil。 countLabel = dora.NewLabel("sarasa-mono-sc-regular", 40, false) if countLabel == nil { panic("创建 Label 失败,可能是字体名不对") } countLabel.SetPosition(dora.NewVec2(90, 90)) // 方块本地坐标的中心 square.AddChild(countLabel.Node) hint := dora.NewLabel("sarasa-mono-sc-regular", 26, false) if hint != nil { // label.SetText(内容):设置要显示的文字。 hint.SetText("点方块:换色并保存") hint.SetPosition(dora.NewVec2(0, -130)) ui.AddChild(hint.Node) } // square.OnTapped(func(touch){...}):注册一个回调,完整点击(按下并松开)后执行。 square.OnTapped(func(touch: dora.Touch) { _ = touch // 这一步用不到触点位置,先标记一下(凹语言要求变量被使用) squareClicked = !squareClicked applySquareState() // 更新颜色和文字 saveState() // 存进文件 }) // ---- 按钮:点击把方块向右移动 --------------------------------- // makeButton 在文件下面,负责做一个"矩形底 + 文字"的按钮。 moveBtn := makeButton("向右移动 →", 0, -230) ui.AddChild(moveBtn) moveBtn.OnTapped(func(touch: dora.Touch) { _ = touch p := square.GetPosition() // square.GetPosition():读节点当前坐标 p.X = p.X + 40 // X 加 40 = 往右走 40 个坐标单位 square.SetPosition(p) }) applySquareState() // 先把方块的初始颜色/文字设好}// 第 3 步:绘制图片(背景 + 僵尸)func Step3() { ui := dora.Director.GetUI() // dora.App.GetVisualSize():返回当前屏幕的宽高(Size)。 size := dora.App.GetVisualSize() // dora.NewSpriteWithFile(图片路径):用图片文件创建一个精灵(能显示图片)。 // 文件不存在时返回 nil,所以一定要判断。 // 注意:文件名是 backgroundpng.png(如果改成了 background.png,这里也要改)。 bg := dora.NewSpriteWithFile("Image/backgroundpng.png") if bg == nil { panic("找不到背景图 Image/backgroundpng.png") } bg.SetSize(size) // 把背景图拉伸成和屏幕一样大 bg.SetAnchor(dora.NewVec2(0.5, 0.5)) bg.SetPosition(dora.Vec2Zero) ui.AddChild(bg.Node) zombie := dora.NewSpriteWithFile("Image/zombie.png") if zombie == nil { panic("找不到 Image/zombie.png") } zombie.SetAnchor(dora.NewVec2(0.5, 0.5)) zombie.SetPosition(dora.NewVec2(240, -60)) // 放在场景右边 ui.AddChild(zombie.Node)}// 第 4 步:存档(读)。func Step4() { loadState()}// 做一个按钮:矩形底 + 文字,返回这个节点。func makeButton(text: string, x: f32, y: f32) => dora.Node { btn := dora.NewNode() btn.SetSize(dora.NewSize(220, 70)) btn.SetAnchor(dora.NewVec2(0.5, 0.5)) btn.SetPosition(dora.NewVec2(x, y)) btn.SetTouchEnabled(true) bg := dora.NewDrawNode() bg.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(220, 70)), dora.NewColor(210, 210, 210, 255), 0, dora.ColorTransparent, ) btn.AddChild(bg.Node) label := dora.NewLabel("sarasa-mono-sc-regular", 28, false) if label != nil { label.SetText(text) label.SetPosition(dora.NewVec2(110, 35)) // 按钮中心(子节点左下角为原点) btn.AddChild(label.Node) } return btn}// 按 squareClicked 更新方块颜色和文字。func applySquareState() { if squareClicked { squareFill.SetColor(dora.NewColor(255, 150, 70, 255)) // 橙色 countLabel.SetText("点到了!") } else { squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // 蓝色 countLabel.SetText("点我") }}// 存档:把状态写进文件("1"=点过,"0"=没点过)。func saveState() { // dora.Content.Save(文件名, 文字内容):把文字写进文件,返回是否成功。 if squareClicked { dora.Content.Save(saveFile, "1") } else { dora.Content.Save(saveFile, "0") }}// 读档:如果文件存在就恢复状态。func loadState() { // dora.Content.Exist(文件名):判断文件/目录是否存在。 // 先判断一下,避免 Load 一个不存在的文件(会报错)。 if dora.Content.Exist(saveFile) { // dora.Content.Load(文件名):读文件里的文字,文件不存在返回 nil。 loaded := dora.Content.Load(saveFile) if loaded != nil && *loaded == "1" { squareClicked = true } else { squareClicked = false } } else { squareClicked = false } applySquareState()}// 用绝对路径让读写指向同一个文件,不依赖资源搜索路径。func writablePath(filename: string) => string { paths := []string{dora.Content.GetWritablePath(), filename} return dora.Path.Concat(&paths)} Full source main3.wa// 社区示例与美术素材 / Community examples and pixel art: serci_loser// ===================================================================// 存档/读档按钮 + 长按按钮 + 键盘(W 上移)// 学什么:// 存档按钮 / 读档按钮(把颜色和位置写进文件、再读回来)// 长按按钮(按住持续向左移动)// 键盘检测(按住 W 持续向上移动)// ===================================================================//// 和 lua(LÖVE) 的对照:// love.filesystem.write / read → dora.Content.Save / Load// love.touchpressed + touchreleased → Node.OnTapBegan / OnTapEnded// love.keyboard.isDown("w") → dora.Keyboard.IsKeyDown(dora.KeyW)//// 运行后:背景 + 僵尸 + 蓝色方块。// 点方块:换颜色。// 按 W:方块持续向上移动。// 按住"按住←左移"按钮:方块持续向左移动。// 点"向右一步":方块往右走 40 个坐标单位。// 点"存档":把颜色和位置存进文件;点"读档":恢复;重启游戏会自动读档。import ( "dora" "strconv" "strings")// 全局变量global squareFill: dora.DrawNode // 方块底色global square: dora.Node // 方块节点global squareClicked = false // 方块状态:false=没点过(蓝),true=点过(橙)global countLabel: *dora.Label // 方块上的文字global statusLabel: *dora.Label // 底部状态/提示文字global longPressLeft = false // "按住←左移"按钮是否正被按住// 同一份存档保存颜色和坐标。global saveFile = "wa-tutorial-3-square.txt"// 入口。调用顺序 = 绘制顺序:先挂的在下层。func init { saveFile = writablePath(saveFile) Step3() // 先画背景、僵尸 Step2() // 再画方块 Step4() // 最后放按钮、主循环、自动读档}// 第 2 步:方块、文字、点击(点一下换颜色)。func Step2() { ui := dora.Director.GetUI() // 拿到 2D 界面根节点,之后所有东西都挂它下面 // dora.NewNode():创建一个空节点,本身看不见,用来当容器或点击区域。 square = dora.NewNode() // SetSize(宽, 高):设置节点矩形大小,同时也当点击区域。 square.SetSize(dora.NewSize(180, 180)) // dora.NewVec2(x, y):创建一个坐标点。 // SetAnchor(x, y):锚点,即节点矩形上哪个点落在 SetPosition 的坐标上;(0.5,0.5)=中心。 square.SetAnchor(dora.NewVec2(0.5, 0.5)) // SetPosition(x, y):放到指定坐标。原点在【屏幕中心】,X 右正,Y 上正。 square.SetPosition(dora.Vec2Zero) // Vec2Zero = (0,0) square.SetTouchEnabled(true) // 打开触摸,才能收到点击 ui.AddChild(square) // AddChild(子节点):挂到界面上才会显示 // dora.NewDrawNode():创建一个画板节点,用来画图形。 squareFill = dora.NewDrawNode() // DrawRect(矩形, 填充色, 描边宽, 描边色):画矩形,描边宽 0 = 不要描边。 // NewRect(左下角坐标, 尺寸):定义矩形区域;NewSize(宽,高):定义尺寸。 // NewColor(红,绿,蓝,不透明):各是 0~255;ColorTransparent = 全透明。 // 有尺寸节点的子节点,坐标原点是它的【左下角】,所以从 (0,0) 画 180×180 正好铺满。 squareFill.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(180, 180)), dora.NewColor(255, 255, 255, 255), 0, dora.ColorTransparent, ) square.AddChild(squareFill.Node) // SetColor(颜色):给绘制节点染色,白色矩形被染成蓝色。 squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // NewLabel(字体名, 字号, 是否SDF):创建文字,找不到字体会返回 nil。 countLabel = dora.NewLabel("sarasa-mono-sc-regular", 40, false) if countLabel == nil { panic("创建 Label 失败,可能是字体名不对") } countLabel.SetPosition(dora.NewVec2(90, 90)) // 方块本地坐标的中心 square.AddChild(countLabel.Node) // OnTapped(func(touch){...}):完整点击(按下并松开)后执行。 square.OnTapped(func(touch: dora.Touch) { _ = touch squareClicked = !squareClicked applySquareState() }) applySquareState() // 先设好初始颜色/文字}// 第 3 步:绘制图片(背景 + 僵尸)。func Step3() { ui := dora.Director.GetUI() size := dora.App.GetVisualSize() // GetVisualSize():当前屏幕宽高 // NewSpriteWithFile(图片路径):用图片创建精灵,文件不存在返回 nil。 bg := dora.NewSpriteWithFile("Image/backgroundpng.png") if bg == nil { panic("找不到背景图 Image/backgroundpng.png") } bg.SetSize(size) // 把背景拉伸成和屏幕一样大 bg.SetAnchor(dora.NewVec2(0.5, 0.5)) bg.SetPosition(dora.Vec2Zero) ui.AddChild(bg.Node) zombie := dora.NewSpriteWithFile("Image/zombie.png") if zombie == nil { panic("找不到 Image/zombie.png") } zombie.SetAnchor(dora.NewVec2(0.5, 0.5)) zombie.SetPosition(dora.NewVec2(240, -60)) ui.AddChild(zombie.Node)}// 第 4 步:按钮 + 主循环 + 自动读档。func Step4() { ui := dora.Director.GetUI() statusLabel = dora.NewLabel("sarasa-mono-sc-regular", 24, false) if statusLabel != nil { statusLabel.SetText("W=上移 | 点方块换色 | 用下面的按钮存档/读档") statusLabel.SetPosition(dora.NewVec2(0, -130)) ui.AddChild(statusLabel.Node) } // ---- 4 个按钮 ------------------------------------------------- // 1) 长按按钮:按住时持续向左移动。 leftBtn := makeButton("按住←左移", -270, -250) ui.AddChild(leftBtn) // OnTapBegan:手指按下瞬间触发;OnTapEnded:手指松开瞬间触发。 // 用它们记录"是否正被按住",真正移动放到下面的主循环里。 leftBtn.OnTapBegan(func(touch: dora.Touch) { _ = touch longPressLeft = true }) leftBtn.OnTapEnded(func(touch: dora.Touch) { _ = touch longPressLeft = false }) // 2) 向右一步按钮。 rightBtn := makeButton("向右一步", -90, -250) ui.AddChild(rightBtn) rightBtn.OnTapped(func(touch: dora.Touch) { _ = touch p := square.GetPosition() // GetPosition():读节点当前坐标 p.X = p.X + 40 // 往右走 40 个坐标单位 square.SetPosition(p) }) // 3) 存档按钮。 saveBtn := makeButton("存档", 90, -250) ui.AddChild(saveBtn) saveBtn.OnTapped(func(touch: dora.Touch) { _ = touch saved := saveState() if statusLabel != nil { if saved { statusLabel.SetText("已存档") } else { statusLabel.SetText("存档写入失败") } } }) // 4) 读档按钮。 loadBtn := makeButton("读档", 270, -250) ui.AddChild(loadBtn) loadBtn.OnTapped(func(touch: dora.Touch) { _ = touch loaded := loadState() if statusLabel != nil { if loaded { statusLabel.SetText("已读档") } else { statusLabel.SetText("没有可读的存档") } } }) // ---- 主循环 + 自动读档 ---------------------------------------- loadState() // 启动时先自动读档,恢复上次保存的状态 // Schedule(func(dt){...}):每帧调用一次,dt=这一帧的秒数;返回 false 表示下一帧继续。 square.Schedule(func(dt: f64) => bool { speed := f32(320) * f32(dt) // 每秒 320 个坐标单位 p := square.GetPosition() // Keyboard.IsKeyDown(键名):按住时每帧为 true。 // KeyW 就是键盘上的 W 键。 if dora.Keyboard.IsKeyDown(dora.KeyW) { p.Y = p.Y + speed // 向上 } if longPressLeft { // 长按按钮被按住 p.X = p.X - speed // 向左 } square.SetPosition(p) return false })}// 做一个按钮:矩形底 + 文字,返回这个节点。func makeButton(text: string, x: f32, y: f32) => dora.Node { btn := dora.NewNode() btn.SetSize(dora.NewSize(160, 60)) btn.SetAnchor(dora.NewVec2(0.5, 0.5)) btn.SetPosition(dora.NewVec2(x, y)) btn.SetTouchEnabled(true) bg := dora.NewDrawNode() bg.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(160, 60)), dora.NewColor(210, 210, 210, 255), 0, dora.ColorTransparent, ) btn.AddChild(bg.Node) label := dora.NewLabel("sarasa-mono-sc-regular", 24, false) if label != nil { label.SetText(text) label.SetPosition(dora.NewVec2(80, 30)) // 按钮中心(子节点左下角为原点) btn.AddChild(label.Node) } return btn}// 按 squareClicked 更新方块颜色和文字。func applySquareState() { if squareClicked { squareFill.SetColor(dora.NewColor(255, 150, 70, 255)) // 橙色 countLabel.SetText("点到了!") } else { squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // 蓝色 countLabel.SetText("点我") }}// 存档格式:颜色,X,Y。一次写入同一份方块状态。func saveState() => bool { color := "0" if squareClicked { color = "1" } p := square.GetPosition() x := strconv.FormatInt(int64(i32(p.X)), 10) y := strconv.FormatInt(int64(i32(p.Y)), 10) return dora.Content.Save(saveFile, color+","+x+","+y)}// 只有三个字段都有效时才恢复方块状态。func loadState() => bool { if !dora.Content.Exist(saveFile) { return false } loaded := dora.Content.Load(saveFile) if loaded == nil { return false } fields := strings.Split(*loaded, ",") if len(fields) != 3 || (fields[0] != "0" && fields[0] != "1") { return false } x, xErr := strconv.ParseInt(fields[1], 10, 32) y, yErr := strconv.ParseInt(fields[2], 10, 32) if xErr != nil || yErr != nil { return false } squareClicked = fields[0] == "1" square.SetPosition(dora.NewVec2(f32(x), f32(y))) applySquareState() return true}// 用绝对路径让读写指向同一个文件,不依赖资源搜索路径。func writablePath(filename: string) => string { paths := []string{dora.Content.GetWritablePath(), filename} return dora.Path.Concat(&paths)} Full source main4.wa// 社区示例与美术素材 / Community examples and pixel art: serci_loser// ===================================================================// 凹语言 × Dora SSR 小教程// 当前运行:第 5 步(文件操作)+ 第 6 步(文本输入框)// 之前的游戏在第 2~4 步(Step2/Step3/Step4),想看就把 init 改成调用它们// ===================================================================//// 运行后:// 第 5 步 在 wa-tutorial-file-demo/ 目录里 创建/读/写/删 一个文件,结果打印在屏幕上。// 第 6 步 点一下输入框再打字;退格=删最后一个字,回车=清空。import ( "dora" "strconv" "strings")// 全局变量global squareFill: dora.DrawNode // 方块底色global square: dora.Node // 方块节点global squareClicked = false // 方块状态:false=没点过(蓝),true=点过(橙)global countLabel: *dora.Label // 方块上的文字global statusLabel: *dora.Label // 底部状态/提示文字global longPressLeft = false // "按住←左移"按钮是否正被按住// 第 6 步(文本输入框)用的状态。global inputText = "" // 输入框里的文字global inputLabel: *dora.Label // 输入框里显示文字的节点// 同一份存档保存颜色和坐标。global saveFile = "wa-tutorial-4-square.txt"// 入口。调用顺序 = 绘制顺序:先挂的在下层。func init { saveFile = writablePath(saveFile) // 想看之前的游戏,把下面换成: Step3(); Step2(); Step4() Step5() // 文件操作 Step6() // 文本输入框}// 第 2 步:方块、文字、点击(点一下换颜色)。func Step2() { ui := dora.Director.GetUI() // 拿到 2D 界面根节点,之后所有东西都挂它下面 // dora.NewNode():创建一个空节点,本身看不见,用来当容器或点击区域。 square = dora.NewNode() // SetSize(宽, 高):设置节点矩形大小,同时也当点击区域。 square.SetSize(dora.NewSize(180, 180)) // dora.NewVec2(x, y):创建一个坐标点。 // SetAnchor(x, y):锚点,即节点矩形上哪个点落在 SetPosition 的坐标上;(0.5,0.5)=中心。 square.SetAnchor(dora.NewVec2(0.5, 0.5)) // SetPosition(x, y):放到指定坐标。原点在【屏幕中心】,X 右正,Y 上正。 square.SetPosition(dora.Vec2Zero) // Vec2Zero = (0,0) square.SetTouchEnabled(true) // 打开触摸,才能收到点击 ui.AddChild(square) // AddChild(子节点):挂到界面上才会显示 // dora.NewDrawNode():创建一个画板节点,用来画图形。 squareFill = dora.NewDrawNode() // DrawRect(矩形, 填充色, 描边宽, 描边色):画矩形,描边宽 0 = 不要描边。 // NewRect(左下角坐标, 尺寸):定义矩形区域;NewSize(宽,高):定义尺寸。 // NewColor(红,绿,蓝,不透明):各是 0~255;ColorTransparent = 全透明。 // 有尺寸节点的子节点,坐标原点是它的【左下角】,所以从 (0,0) 画 180×180 正好铺满。 squareFill.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(180, 180)), dora.NewColor(255, 255, 255, 255), 0, dora.ColorTransparent, ) square.AddChild(squareFill.Node) // SetColor(颜色):给绘制节点染色,白色矩形被染成蓝色。 squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // NewLabel(字体名, 字号, 是否SDF):创建文字,找不到字体会返回 nil。 countLabel = dora.NewLabel("sarasa-mono-sc-regular", 40, false) if countLabel == nil { panic("创建 Label 失败,可能是字体名不对") } countLabel.SetPosition(dora.NewVec2(90, 90)) // 方块本地坐标的中心 square.AddChild(countLabel.Node) // OnTapped(func(touch){...}):完整点击(按下并松开)后执行。 square.OnTapped(func(touch: dora.Touch) { _ = touch squareClicked = !squareClicked applySquareState() }) applySquareState() // 先设好初始颜色/文字}// 第 3 步:绘制图片(背景 + 僵尸)。func Step3() { ui := dora.Director.GetUI() size := dora.App.GetVisualSize() // GetVisualSize():当前屏幕宽高 // NewSpriteWithFile(图片路径):用图片创建精灵,文件不存在返回 nil。 bg := dora.NewSpriteWithFile("Image/backgroundpng.png") if bg == nil { panic("找不到背景图 Image/backgroundpng.png") } bg.SetSize(size) // 把背景拉伸成和屏幕一样大 bg.SetAnchor(dora.NewVec2(0.5, 0.5)) bg.SetPosition(dora.Vec2Zero) ui.AddChild(bg.Node) zombie := dora.NewSpriteWithFile("Image/zombie.png") if zombie == nil { panic("找不到 Image/zombie.png") } zombie.SetAnchor(dora.NewVec2(0.5, 0.5)) zombie.SetPosition(dora.NewVec2(240, -60)) ui.AddChild(zombie.Node)}// 第 4 步:按钮 + 主循环 + 自动读档。func Step4() { ui := dora.Director.GetUI() statusLabel = dora.NewLabel("sarasa-mono-sc-regular", 24, false) if statusLabel != nil { statusLabel.SetText("W=上移 | 点方块换色 | 用下面的按钮存档/读档") statusLabel.SetPosition(dora.NewVec2(0, -130)) ui.AddChild(statusLabel.Node) } // ---- 4 个按钮 ------------------------------------------------- // 1) 长按按钮:按住时持续向左移动。 leftBtn := makeButton("按住←左移", -270, -250) ui.AddChild(leftBtn) // OnTapBegan:手指按下瞬间触发;OnTapEnded:手指松开瞬间触发。 // 用它们记录"是否正被按住",真正移动放到下面的主循环里。 leftBtn.OnTapBegan(func(touch: dora.Touch) { _ = touch longPressLeft = true }) leftBtn.OnTapEnded(func(touch: dora.Touch) { _ = touch longPressLeft = false }) // 2) 向右一步按钮。 rightBtn := makeButton("向右一步", -90, -250) ui.AddChild(rightBtn) rightBtn.OnTapped(func(touch: dora.Touch) { _ = touch p := square.GetPosition() // GetPosition():读节点当前坐标 p.X = p.X + 40 // 往右走 40 个坐标单位 square.SetPosition(p) }) // 3) 存档按钮。 saveBtn := makeButton("存档", 90, -250) ui.AddChild(saveBtn) saveBtn.OnTapped(func(touch: dora.Touch) { _ = touch saved := saveState() if statusLabel != nil { if saved { statusLabel.SetText("已存档") } else { statusLabel.SetText("存档写入失败") } } }) // 4) 读档按钮。 loadBtn := makeButton("读档", 270, -250) ui.AddChild(loadBtn) loadBtn.OnTapped(func(touch: dora.Touch) { _ = touch loaded := loadState() if statusLabel != nil { if loaded { statusLabel.SetText("已读档") } else { statusLabel.SetText("没有可读的存档") } } }) // ---- 主循环 + 自动读档 ---------------------------------------- loadState() // 启动时先自动读档,恢复上次保存的状态 // Schedule(func(dt){...}):每帧调用一次,dt=这一帧的秒数;返回 false 表示下一帧继续。 square.Schedule(func(dt: f64) => bool { speed := f32(320) * f32(dt) // 每秒 320 个坐标单位 p := square.GetPosition() // Keyboard.IsKeyDown(键名):按住时每帧为 true。 // KeyW 就是键盘上的 W 键。 if dora.Keyboard.IsKeyDown(dora.KeyW) { p.Y = p.Y + speed // 向上 } if longPressLeft { // 长按按钮被按住 p.X = p.X - speed // 向左 } square.SetPosition(p) return false })}// 做一个按钮:矩形底 + 文字,返回这个节点。func makeButton(text: string, x: f32, y: f32) => dora.Node { btn := dora.NewNode() btn.SetSize(dora.NewSize(160, 60)) btn.SetAnchor(dora.NewVec2(0.5, 0.5)) btn.SetPosition(dora.NewVec2(x, y)) btn.SetTouchEnabled(true) bg := dora.NewDrawNode() bg.DrawRect( dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(160, 60)), dora.NewColor(210, 210, 210, 255), 0, dora.ColorTransparent, ) btn.AddChild(bg.Node) label := dora.NewLabel("sarasa-mono-sc-regular", 24, false) if label != nil { label.SetText(text) label.SetPosition(dora.NewVec2(80, 30)) // 按钮中心(子节点左下角为原点) btn.AddChild(label.Node) } return btn}// 按 squareClicked 更新方块颜色和文字。func applySquareState() { if squareClicked { squareFill.SetColor(dora.NewColor(255, 150, 70, 255)) // 橙色 countLabel.SetText("点到了!") } else { squareFill.SetColor(dora.NewColor(80, 180, 255, 255)) // 蓝色 countLabel.SetText("点我") }}// 存档格式:颜色,X,Y。一次写入同一份方块状态。func saveState() => bool { color := "0" if squareClicked { color = "1" } p := square.GetPosition() x := strconv.FormatInt(int64(i32(p.X)), 10) y := strconv.FormatInt(int64(i32(p.Y)), 10) return dora.Content.Save(saveFile, color+","+x+","+y)}// 只有三个字段都有效时才恢复方块状态。func loadState() => bool { if !dora.Content.Exist(saveFile) { return false } loaded := dora.Content.Load(saveFile) if loaded == nil { return false } fields := strings.Split(*loaded, ",") if len(fields) != 3 || (fields[0] != "0" && fields[0] != "1") { return false } x, xErr := strconv.ParseInt(fields[1], 10, 32) y, yErr := strconv.ParseInt(fields[2], 10, 32) if xErr != nil || yErr != nil { return false } squareClicked = fields[0] == "1" square.SetPosition(dora.NewVec2(f32(x), f32(y))) applySquareState() return true}// ===================================================================// 第 5 步:文件操作(在指定位置 创建 / 读 / 写 / 删)// ===================================================================func Step5() { ui := dora.Director.GetUI() addLabel(ui, "第 5 步:文件操作", 32, 0, 260) // 使用教程专用的目录,并防止覆盖或删除已有文件。 dir := writablePath("wa-tutorial-file-demo") file := writablePath("wa-tutorial-file-demo/data.txt") if !dora.Content.Isdir(dir) && !dora.Content.Mkdir(dir) { addLabel(ui, "创建目录失败", 24, 0, 220) return } if dora.Content.Exist(file) { addLabel(ui, "文件已存在,跳过演示", 24, 0, 220) return } if !dora.Content.Save(file, "你好,凹语言!") { addLabel(ui, "写入失败", 24, 0, 220) return } loaded := dora.Content.Load(file) if loaded != nil { addLabel(ui, "读到了: "+*loaded, 24, 0, 220) } if dora.Content.Remove(file) && !dora.Content.Exist(file) { addLabel(ui, "已删除演示文件", 24, 0, 185) } else { addLabel(ui, "删除演示文件失败", 24, 0, 185) }}// ===================================================================// 第 6 步:文本输入框(输入文字 + 退格删除文字)// ===================================================================func Step6() { ui := dora.Director.GetUI() addLabel(ui, "第 6 步:文本输入框", 32, 0, 115) addLabel(ui, "点一下输入框再打字;退格=删最后一个字,回车=清空", 24, 0, 80) inputText = "" // 输入框:一个带尺寸的节点 + 深色矩形底 + 文字。 box := dora.NewNode() box.SetSize(dora.NewSize(560, 80)) box.SetAnchor(dora.NewVec2(0.5, 0.5)) box.SetPosition(dora.NewVec2(0, -20)) box.SetTouchEnabled(true) ui.AddChild(box) bg := dora.NewDrawNode() bg.DrawRect(dora.NewRect(dora.NewVec2(0, 0), dora.NewSize(560, 80)), dora.NewColor(45, 45, 70, 255), 0, dora.ColorTransparent) box.AddChild(bg.Node) inputLabel = dora.NewLabel("sarasa-mono-sc-regular", 30, false) if inputLabel == nil { panic("创建 Label 失败") } inputLabel.SetText("点这里输入…") inputLabel.SetPosition(dora.NewVec2(280, 40)) box.AddChild(inputLabel.Node) // 点一下输入框:挂载输入法(手机弹软键盘;桌面则聚焦后开始接收文字)。 box.OnTapped(func(touch: dora.Touch) { _ = touch box.AttachIme() // AttachIme():让这个节点能收到 TextInput 事件 }) // OnTextInput(func(text)):每敲出一个字,就把这个字(字符串)送进来,我们追加到末尾。 box.OnTextInput(func(text: string) { inputText = inputText + text updateInputLabel() }) // 退格/回车用主循环检测。 box.Schedule(func(dt: f64) => bool { // IsKeyPressed:按下瞬间只触发一次(适合"按一下删一个字")。 if dora.Keyboard.IsKeyPressed(dora.KeyBackSpace) { inputText = removeLastChar(inputText) // 删除最后一个字 updateInputLabel() } if dora.Keyboard.IsKeyPressed(dora.KeyReturn) { inputText = "" // 回车 = 清空 updateInputLabel() } return false })}// 更新输入框里显示的文字。func updateInputLabel() { if inputLabel != nil { if inputText == "" { inputLabel.SetText("(空)") } else { inputLabel.SetText(inputText) } }}// 删除字符串最后一个"字符"(正确处理中文等多字节字符)。func removeLastChar(s: string) => string { n := len(s) if n == 0 { return s } i := n - 1 // UTF-8 里,连续字节是 10xxxxxx(0x80~0xBF)。往前跳过连续字节,找到最后一个字符的开头。 for i > 0 && (s[i]&0xC0) == 0x80 { i-- } return s[:i]}// 快速创建一个文字标签并挂到 parent 下(x,y 是文字中心坐标)。func addLabel(parent: dora.Node, text: string, size: i32, x: f32, y: f32) => *dora.Label { label := dora.NewLabel("sarasa-mono-sc-regular", size, false) if label == nil { return nil } label.SetText(text) label.SetPosition(dora.NewVec2(x, y)) parent.AddChild(label.Node) return label}// 用绝对路径让读写指向同一个文件,不依赖资源搜索路径。func writablePath(filename: string) => string { paths := []string{dora.Content.GetWritablePath(), filename} return dora.Path.Concat(&paths)}